package com.meirengu.webview.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.HttpUtil;
import com.meirengu.webview.dao.KwChannelDataDao;
import com.meirengu.webview.model.KwChannelData;
import com.meirengu.webview.service.KwChannelDataService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * KwChannelData服务实现层
 *
 * @author maoruxin
 * @create Sat Dec 16 15:33:39 CST 2017
 */
@Service
public class KwChannelDataServiceImpl extends BaseServiceImpl<KwChannelData> implements KwChannelDataService {

    @Autowired
    private KwChannelDataDao kwChannelDataDao;

    private static final Logger logger = LoggerFactory.getLogger(KwChannelDataServiceImpl.class);

    /**
     * 申请产品
     *
     * @param channelId
     * @return
     */
    public int updateApplyNum(String channelId, boolean O2Oflag) {
        if (StringUtils.isNotBlank(channelId)) {
            Map<String, Object> map = new HashMap<>();
            map.put("channelId", channelId);
            map.put("applyNum", "1");
            if (O2Oflag) {
                map.put("O2ONum", "1");
            }
            return kwChannelDataDao.addNum(map);
        }
        return 0;
    }

    /**
     * 根据日期去秒单之家获取渠道数据
     *
     * @param dataDate
     */
    public void getMDZJData(String dataDate) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(dataDate)) {
            Date yestday = org.apache.commons.lang3.time.DateUtils.addDays(now, -1);
            dataDate = sdf.format(yestday);
        }
        logger.info("获取秒单之家数据日期:{}", dataDate);
        Map<String, String> param = new HashMap();
        param.put("data_date", dataDate);
        param.put("page_num", "1");
        param.put("page_size", "1000");
        param.put("create_by", "kwd");

        String url = ConfigUtil.getConfig("mdzh.channel.url");

        HttpUtil.HttpResult httpResult = HttpUtil.doGet(url, param);
        if (httpResult.getStatusCode() == HttpStatus.SC_OK) {
            JSONObject resultJson = JSON.parseObject(httpResult.getContent());
            int code = resultJson.getIntValue("code");
            JSONObject data = resultJson.getJSONObject("data");
            if (code == 200) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
                for (int i = 0; i < list.size(); i++) {
                    Map<String, Object> map = list.get(i);
                    String channelId = map.get("channelId").toString();
                    String channelNo = map.get("channelNo").toString();
                    String channelName = map.get("channelName").toString();
                    Integer sellNum = Integer.parseInt(map.get("sellNum").toString());
                    Integer refundNum = Integer.parseInt(map.get("refundNum").toString());
                    Integer sellAmount = Integer.parseInt(map.get("sellAmount").toString().substring(0, map.get("sellAmount").toString().length() - 3));
                    System.out.println("channelId:" + channelId + " sellNum:" + sellNum + " refundNum:" + refundNum);
                    Map<String, Object> queryMap = new HashMap<>();
                    queryMap.put("channelId", channelId);
                    queryMap.put("showRegistNum", sellNum);
                    queryMap.put("showApplyNum", refundNum);
                    queryMap.put("sellAmount", sellAmount);
                    queryMap.put("dataDate", dataDate);
                    int count = kwChannelDataDao.addMDZJNum(queryMap);
                    if (count != 1) {
                        logger.error("更新失败 ");
                    }
                }

            } else {
                logger.error("请求秒单之家内部服务 出错 code {} err:{}", httpResult.getStatusCode(), httpResult.getContent());
            }
        } else {
            logger.error("请求秒单之家Http 出错: code {} err:{}", httpResult.getStatusCode(), httpResult.getContent());
        }

    }

    @Override
    public int addNum(Map map) {
        return kwChannelDataDao.addNum(map);
    }
}
